package cn.ming.application.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.ming.application.entity.Order;
import cn.ming.application.mapper.OrderMapper;
import cn.ming.application.vo.SeparateTestVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuming
 * @version 1.0.0
 * @description 业务类
 * @since 2022-11-19 14:33
 */
@RestController
public class SeparateTestController {

    @Autowired
    private OrderMapper orderMapper;

    @GetMapping("/insert")
    public String insert() {
        for (int i = 0; i < 10; i++) {
            long userId = i;
            long orderNo = i + 1;
            Order order = new Order();
            order.setName("阿离");
            order.setUserId(userId);
            order.setOrderNo(orderNo);
            order.setPayTime(LocalDateTime.now());
            order.setDescription("131323131");
            orderMapper.addOrder(order);
        }
        return "ok";
    }
    @GetMapping("/query")
    public List<SeparateTestVO> query() {
        List<Order> orders = orderMapper.getOrders();
        if (!CollectionUtils.isEmpty(orders)) {
            return orders.stream().map(order -> {
                SeparateTestVO testVO = new SeparateTestVO();
                BeanUtil.copyProperties(order, testVO);
                testVO.setPayTime(order.getPayTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                return testVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}
